package genedit.gui.propertyDialogs;

import genedit.gui.GMainFrame;
import genedit.gui.exceptions.RequiredFieldException;
import genedit.model.graph.GraphElement;
import genedit.model.graph.classModel.AggregationLink;
import genedit.model.graph.classModel.ClassModel;
import genedit.model.graph.classModel.InterfaceModel;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

@SuppressWarnings("serial")
public class PanelDetailAggregationLink   extends AbstractPanelDetail {
	

	private JLabel lblMultiplicity1;
	private JLabel lblMultiplicity2;
	private JComboBox cmbMultiplicity1;
	private JComboBox cmbMultiplicity2;
	private JLabel lblNavigable1;
	private JLabel lblNavigable2;
	private JCheckBox chkNavigable1;
	private JCheckBox chkNavigable2;
	private JLabel lblRoleName1;
	private JTextField txtRoleName1;
	private JLabel lblRoleName2;
	private JTextField txtRoleName2;
	String[] multiplicity;
	private JLabel lblName;
	private JTextField txtName;
	
	private JButton btnStart;
	private JButton btnEnd;
	protected AggregationLink aggregationLinkEl;
	
	public PanelDetailAggregationLink(GraphElement element) {
		
		aggregationLinkEl = (AggregationLink)element;
		
		setLayout(new BorderLayout());

		Dimension lblDimension = new Dimension(50,20);
		
		Box boxCentar = new Box(BoxLayout.Y_AXIS);
		Box boxCentar1 = new Box(BoxLayout.Y_AXIS);
		Box boxCentar2 = new Box(BoxLayout.Y_AXIS);
		
		JPanel panName = new JPanel(new FlowLayout(FlowLayout.LEFT));
		JPanel panGroups = new JPanel(new FlowLayout(FlowLayout.LEFT));
		lblName = new JLabel(GMainFrame.getInstance().getResourceBundle().getString("lblName"));
		lblName.setPreferredSize(lblDimension);
		txtName = new JTextField(10);
		txtName.setText(aggregationLinkEl.getName());
		panName.add(lblName);
		panName.add(txtName);
		
		
		boxCentar.add(panName);
		
		
		add(boxCentar,BorderLayout.CENTER);
		
	
		btnStart = new JButton(aggregationLinkEl.getStart().getParent().getName());
		btnStart.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				if(aggregationLinkEl.getStart().getParent() instanceof ClassModel){
					ClassModelDialog dialog = new ClassModelDialog(GMainFrame.getInstance(), aggregationLinkEl.getStart().getParent());
					dialog.setVisible(true);
				}
				else if(aggregationLinkEl.getStart().getParent() instanceof InterfaceModel){
					InterfaceModelDialog dialog = new InterfaceModelDialog(GMainFrame.getInstance(), aggregationLinkEl.getStart().getParent());
					dialog.setVisible(true);
				}
				}
		});
		
		btnEnd = new JButton(aggregationLinkEl.getEnd().getParent().getName());
		btnEnd.addActionListener(new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent arg0) {
				if(aggregationLinkEl.getEnd().getParent() instanceof ClassModel){
					ClassModelDialog dialog = new ClassModelDialog(GMainFrame.getInstance(), aggregationLinkEl.getEnd().getParent());
					dialog.setVisible(true);
				}
				else if(aggregationLinkEl.getEnd().getParent() instanceof InterfaceModel){
					InterfaceModelDialog dialog = new InterfaceModelDialog(GMainFrame.getInstance(), aggregationLinkEl.getEnd().getParent());
					dialog.setVisible(true);
				}
			}
		});
		
		boxCentar1.add(btnStart);
		boxCentar2.add(btnEnd);

		JPanel panRoleName1 = new JPanel(new FlowLayout(FlowLayout.LEFT));
		lblRoleName1 = new JLabel(GMainFrame.getInstance().getResourceBundle().getString("lblRoleName"));
		lblRoleName1.setPreferredSize(new Dimension(80, 20));
		txtRoleName1 = new JTextField(10);
		txtRoleName1.setText(aggregationLinkEl.getRoleNameStart());
		panRoleName1.add(lblRoleName1);
		panRoleName1.add(txtRoleName1);
		boxCentar1.add(panRoleName1);
		
		JPanel panRoleName2 = new JPanel(new FlowLayout(FlowLayout.LEFT));
		lblRoleName2 = new JLabel(GMainFrame.getInstance().getResourceBundle().getString("lblRoleName"));
		lblRoleName2.setPreferredSize(new Dimension(80, 20));
		txtRoleName2 = new JTextField(10);
		txtRoleName2.setText(aggregationLinkEl.getRoleNameEnd());
		panRoleName2.add(lblRoleName2);
		panRoleName2.add(txtRoleName2);
		boxCentar2.add(panRoleName2);
		
		JPanel panMultiplicity1 = new JPanel(new FlowLayout(FlowLayout.LEFT));
		JPanel panMultiplicity2 = new JPanel(new FlowLayout(FlowLayout.LEFT));
		lblMultiplicity1 = new JLabel(GMainFrame.getInstance().getResourceBundle().getString("lblMultiplicity"));
		lblMultiplicity2 = new JLabel(GMainFrame.getInstance().getResourceBundle().getString("lblMultiplicity"));
		lblMultiplicity1.setPreferredSize(new Dimension(80, 20));
		lblMultiplicity2.setPreferredSize(new Dimension(80, 20));
		multiplicity = new String [] {"0..1", "0..*", "1..1", "1..*", "*"};
		
		cmbMultiplicity1 = new JComboBox(multiplicity);
		cmbMultiplicity1.setSelectedItem(aggregationLinkEl.getMultiplicityStart());
		panMultiplicity1.add(lblMultiplicity1);
		panMultiplicity1.add(cmbMultiplicity1);
		boxCentar1.add(panMultiplicity1);
		
		cmbMultiplicity2 = new JComboBox(multiplicity);
		cmbMultiplicity2.setSelectedItem(aggregationLinkEl.getMultiplicityEnd());
		panMultiplicity2.add(lblMultiplicity2);
		panMultiplicity2.add(cmbMultiplicity2);
		boxCentar2.add(panMultiplicity2);
		
		boxCentar1.setAlignmentX(LEFT_ALIGNMENT);
		boxCentar2.setAlignmentX(LEFT_ALIGNMENT);
		
		panGroups.add(boxCentar1);
		panGroups.add(boxCentar2);
		
		boxCentar.add(panGroups);
		
		add(boxCentar,BorderLayout.CENTER);
		

	}
	


	public JTextField getTxtRoleName1() {
		return txtRoleName1;
	}





	public void setTxtRoleName1(JTextField txtRoleName1) {
		this.txtRoleName1 = txtRoleName1;
	}





	public JComboBox getCmbMultiplicity1() {
		return cmbMultiplicity1;
	}



	public void setCmbMultiplicity1(JComboBox cmbMultiplicity1) {
		this.cmbMultiplicity1 = cmbMultiplicity1;
	}



	public JComboBox getCmbMultiplicity2() {
		return cmbMultiplicity2;
	}



	public void setCmbMultiplicity2(JComboBox cmbMultiplicity2) {
		this.cmbMultiplicity2 = cmbMultiplicity2;
	}



	public JTextField getTxtRoleName2() {
		return txtRoleName2;
	}





	public void setTxtRoleName2(JTextField txtRoleName2) {
		this.txtRoleName2 = txtRoleName2;
	}







	public JTextField getTxtName() {
		return txtName;
	}





	public void setTxtName(JTextField txtName) {
		this.txtName = txtName;
	}





	public void proveriIspravnostPodataka() throws RequiredFieldException {
		
		if (txtName.getText().trim().equals("")) {
			txtName.setBackground(Color.RED);
		//	ok = false;
			txtName.addKeyListener(new KeyListener() {
				
				@Override
				public void keyTyped(KeyEvent e) {
					
					
				}
				
				@Override
				public void keyReleased(KeyEvent e) {
					// TODO Auto-generated method stub
					
				}
				
				@Override
				public void keyPressed(KeyEvent e) {
					if (e.isActionKey() || e.getKeyCode()==KeyEvent.VK_ENTER || e.getKeyCode()==KeyEvent.VK_BACK_SPACE)
						return;
					
					else
						txtName.setBackground(Color.WHITE);
			//		ok = true;
					
				}
				
			});
			RequiredFieldException rfe = new RequiredFieldException(
					);
			throw rfe;
		}
		

	}


	public JCheckBox getChkNavigable1() {
		return chkNavigable1;
	}





	public void setChkNavigable1(JCheckBox chkNavigable1) {
		this.chkNavigable1 = chkNavigable1;
	}





	public JCheckBox getChkNavigable2() {
		return chkNavigable2;
	}





	public void setChkNavigable2(JCheckBox chkNavigable2) {
		this.chkNavigable2 = chkNavigable2;
	}


}
